package com.training.api.controller;

import com.training.api.service.LearningTrackDataService;
import com.training.base.Result;
import com.training.bean.LearningTrackData;
import com.training.bean.dao.LearningTrackDataDTO;
import com.training.jwt.JWTUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/learningTrack")
public class LearningTrackDataController {
    
    @Autowired
    private LearningTrackDataService learningTrackDataService;
    
    /**
     * 批量上报学习情况接口
     * @param dataDTOList 学习情况列表
     * @return 上报结果
     */
    @PostMapping("/batch-report")
    public ResponseEntity<Result<String>> batchReportLearningTrackData(@RequestBody List<LearningTrackDataDTO> dataDTOList, HttpServletRequest request) {
        try {
            // 从JWT token中获取用户ID
            int uid = JWTUtils.getUserIdFromRequest(request);
            
            // 转换DTO为实体对象
            List<LearningTrackData> dataList = dataDTOList.stream().map(dto -> {
                LearningTrackData data = new LearningTrackData();
                BeanUtils.copyProperties(dto, data);
                data.setUid(uid); // 设置从token中获取的用户ID
                // 设置创建时间
                if (data.getCread_date() == null) {
                    data.setCread_date(new Date());
                }
                // 更新最后时间
                data.setLast_date(new Date());
                return data;
            }).toList();
            
            learningTrackDataService.saveLearningTrackDataList(dataList);
            return ResponseEntity.ok(new Result<>("学习情况批量上报成功", true, "学习情况批量上报成功"));
        } catch (Exception e) {
            return ResponseEntity.status(500).body(new Result<>(500, "学习情况批量上报失败: " + e.getMessage()));
        }
    }
    
    /**
     * 单条上报学习情况接口
     * @param dataDTO 学习情况
     * @return 上报结果
     */
    @PostMapping("/report")
    public ResponseEntity<Result<LearningTrackData>> reportLearningTrackData(@RequestBody LearningTrackDataDTO dataDTO, HttpServletRequest request) {
        try {
            // 从JWT token中获取用户ID
            int uid = JWTUtils.getUserIdFromRequest(request);
            
            // 转换DTO为实体对象
            LearningTrackData data = new LearningTrackData();
            BeanUtils.copyProperties(dataDTO, data);
            data.setUid(uid); // 设置从token中获取的用户ID
            // 设置创建时间
            if (data.getCread_date() == null) {
                data.setCread_date(new Date());
            }
            // 更新最后时间
            data.setLast_date(new Date());
            
            LearningTrackData savedData = learningTrackDataService.saveOrUpdate(data);
            return ResponseEntity.ok(new Result<>(savedData, true, "学习情况上报成功"));
        } catch (Exception e) {
            return ResponseEntity.status(500).body(new Result<>(500, "学习情况上报失败: " + e.getMessage()));
        }
    }
}